package com.gitee.huanminabc.jtimerwheel.core;


import com.gitee.huanminabc.jtimerwheel.base.*;
import com.gitee.huanminabc.jtimerwheel.enums.TimerStatus;
import com.gitee.huanminabc.jtimerwheel.utils.TimerWheelDateEnum;
import com.gitee.huanminabc.jtimerwheel.utils.TimerWheelDateUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;

/**
 * 简要描述, 之前在c语言中写的,然后用的感觉不错,迁移到java中让后在升级了下,支持分布式功能
 * @Author: huanmin
 * @Date: 2022/11/30 17:05
 * @Version: 1.0
 * @Description: 文件作用详细描述....
 */
@Slf4j
public class TimerWheelManager {

    //修改任务状态,为确认删除
    public static void updateTaskStatusDEL(String timerId) {
        TimerNode timerNode = TimerWheelCore.tasks.get(timerId);
        if (timerNode != null) {
            timerNode.timerStatus = TimerStatus.NOTARIZE_DEL;//修改为确认删除
        }
    }


    //重启任务(前提任务没有被删除)
    public static void UpdateTaskStatusRestart(String timerId) {
        TimerNode timerNode = TimerWheelCore.tasks.get(timerId);
        if (timerNode != null) {
            if (timerNode.timerStatus !=TimerStatus.NOTARIZE_DEL) {
                timerNode.timerStatus =TimerStatus.RESTART;//修改为重启任务
            }
        }
    }

    //修改任务状态为停止(前提任务不能是确认删除)
    public static void updateTaskStatusStop(String timerId) {
        TimerNode timerNode = TimerWheelCore.tasks.get(timerId);
        if (timerNode != null) {
            if (timerNode.timerStatus !=TimerStatus.NOTARIZE_DEL) {
                timerNode.timerStatus = TimerStatus.STOP;//修改为停止
            }
        }
    }


    //修改指定任务的执行计划并重启任务
    public static void updateTaskTimerResolver(String timerId, String strResolver) {
        TimerNode timerNode = TimerWheelCore.tasks.get(timerId);
        if (timerNode != null) {
            timerNode.strResolver = strResolver;
            timerNode.timerStatus =TimerStatus.RESTART;//修改为重启任务
        }
    }


    //任务添加
    public static  void addTimerSask(String timer_resolver, String timerId, Runnable func) {
        //判断任务是否存在
        if (TimerWheelCore.tasks.containsKey(timerId)) {
            throw new TimerWheelException("任务已经存在");
        }
        //创建任务节点
        TimerNode timerNode = TimerWheelCore.createTimerNode(timer_resolver, timerId, func);
        //添加任务到任务列表
        TimerWheelCore.tasks.put(timerId, timerNode);
        //任务添加到时间轮
        TimerWheelCore.addTimerWheel(timerNode);
        //添加任务总数
        TimerWheelCore.addTotal();

    }
    //true 运行中 false 停止
    public static void setStopTimer(boolean stopTimer) {
        TimerWheelCore.runState = stopTimer;
    }

    /**
     * true 启动过期任务补偿 false 不启动   ,默认启动
     * @param outPayTask
     */
    public static void setOutPayTask(boolean outPayTask) {
        TimerWheelCore.outPayTask = outPayTask;
    }

    //是否开始下次任务预测打印
    public static void setNextTaskPrint(boolean nextTaskPrint) {
        TimerWheelCore.nextTaskPrint = nextTaskPrint;
        Date date=new Date();
        String currentDate = TimerWheelDateUtil.dateTurnString(date, TimerWheelDateEnum.DATETIME_PATTERN);
        log.info( "当前时间: "+date.getTime()/1000+"(时间戳)---: " + currentDate+"(格式化时间)\n");
    }
}
